package cn.quickcode.monitor.ui.part;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleFactory;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;

public class ConsoleViewPart implements IConsoleFactory
{
	private static MessageConsole console = new MessageConsole("系统运行日志...", null);

	@Override
	public void openConsole()
	{
		if (console != null)
		{
			IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); // 得到默认控制台管理器
			IConsole[] existing = manager.getConsoles(); // 得到已经存在的
			boolean exists = false;
			for (int i = 0; i < existing.length; i++)
			{
				if (console == existing[i])
					exists = true;
			}
			if (!exists)
			{
				manager.addConsoles(new IConsole[] { console });
			}
			manager.showConsoleView(console);
		}
	}

	public static void closeConsole()
	{
		IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
		if (console != null)
		{
			manager.removeConsoles(new IConsole[] { console });
		}
	}

	public static MessageConsole getConsole()
	{
		return console;
	}

}
